package com.jlect.swebing.ui.client.button;

import com.jlect.swebing.ui.client.ActionListener;
import com.jlect.swebing.ui.client.GComponent;
import com.jlect.swebing.ui.client.Image;
import com.jlect.swebing.ui.client.laf.ButtonUI;
import com.jlect.swebing.ui.client.laf.UIManager;

/**
 * Button component
 *
 * @author Sergey Kozmin
 * @since 11.11.2007 13:07:52
 */
public class GButton extends GComponent {
    private static final String uiComponentID = "GButton";

    public static final String BUTTON_TEXT_PROPERTY_NAME = "button.text";
    public static final String BUTTON_IMAGE_PROPERTY_NAME = "button.image";

    private ButtonBase base;
    private ButtonModel model;

    protected GButton(String text, Image image) {
        ButtonView view = new DefaultButtonViewImpl();
        model = new DefaultButtonModelImpl(this);
        ButtonController controller = new DefaultButtonController();
        base = new ButtonBase(model, view, controller);

        model.setButtonText(text);
        model.setButtonImage(image);
    }

    public GButton(String text) {
        this(text, null);
    }

    public GButton(Image image) {
        this(null, image);
    }

    /*public ButtonRenderer getButtonRenderer() {
        return (ButtonRenderer) getRenderer();
    }

    public void setButtonComponent(ButtonRenderer renderer) {
        setRenderer(renderer);
    }

    protected Renderer createRenderer() {
        ButtonRenderer buttonRenderer = (ButtonRenderer) RenderersConfig.getRenderersFactory().createComponentRenderer(DefaultRenderers.INPUT_BASED_BUTTON.getName());
        return buttonRenderer;
    }*/

    public String getText() {
        return model.getButtonText();
    }

    public void setText(String text) {
        model.setButtonText(text);
    }

    public Image getImage() {
        return model.getButtonImage();
    }

    public void setImage(Image image) {
        model.setButtonImage(image);
    }

    public void addActionListener(ActionListener listener) {
        base.getController().addActionListener(listener);
    }

    public void removeActionListener(ActionListener listener) {
        base.getController().removeActionListener(listener);
    }

    public ButtonUI getButtonUI() {
        return (ButtonUI) getUI();
    }

    public void updateUI() {
        setUI(UIManager.getInstance().getUI(this));
    }

    public String getUiComponentID() {
        return uiComponentID;
    }
}
